package org.nfIt.service.impl;

import cn.hutool.core.date.DateUtil;
import org.nfIt.dto.order.OrderStatisticsDto;
import org.nfIt.entity.order.OrderStatistics;
import org.nfIt.mapper.OrderStatisticsMapper;
import org.nfIt.service.OrderInfoService;
import org.nfIt.vo.order.OrderStatisticsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClossName:OrderInfoServiceImpl
 * Pakage:org.nfIt.service.impl
 * Description
 *
 * @Author L
 * @Create 2024/9/10 19:39
 * @Version 2023.1.3
 */
@Service
public class OrderInfoServiceImpl implements OrderInfoService {

    @Autowired
    private OrderStatisticsMapper orderStatisticsMapper ;

    /**
     *DTO - 封装传过来的数据
     * VO - 封装返回结果
     *
     *实现Echarts图表显示
     * @param orderStatisticsDto 封装传过来的数据
     */
    @Override
    public OrderStatisticsVo getOrderStatisticsData(OrderStatisticsDto orderStatisticsDto) {

        //根据dto条件查询统计结果数据，返回list集合
        List<OrderStatistics> orderStatisticsList = orderStatisticsMapper.selectList(orderStatisticsDto) ;

        //遍历list集合，得到所有日期，把所有日期封装list新集合里面
        List<String> dateList = orderStatisticsList.stream()
                .map(orderStatistics ->
                        //日期格式转换成字符串
                        DateUtil.format(orderStatistics.getOrderDate(), "yyyy-MM-dd"))
                            //加入到新的list集合
                            .collect(Collectors.toList());

        //遍历list集合，得到所有日期对应总金额，把总金额封装list新集合里面
        List<BigDecimal> amountList = orderStatisticsList.stream()
                .map(OrderStatistics::getTotalAmount)
                    .collect(Collectors.toList());

        //把两个list集合封装0rderstatisticsVo，返回orderstatisticsVo
        OrderStatisticsVo orderStatisticsVo = new OrderStatisticsVo() ;
        orderStatisticsVo.setDateList(dateList);
        orderStatisticsVo.setAmountList(amountList);

        // 返回数据
        return orderStatisticsVo;
    }
}
